<?php

class CViewSmarty extends CBase implements IView {
	
	function __construct()
	{
		parent::__construct();
	}
	
	public function render($viewDir,$viewFile,$param=array())
	{
		$smarty = Config::get("viewSet.smarty",false);
		
		if(!$smarty)
		{
			CError::handler(E_ERROR,"smarty配置错误",__FILE__,__LINE__);
		}
		
		$smartyDir = $smarty["dir"];
		$smartyCompileDir = $smarty["compileDir"];

		$smartyDir = $this->getAbsolutePath($smartyDir);
		
		if(is_file($smartyDir."Smarty.class.php"))
		{
			require_once $smartyDir."Smarty.class.php";
			
			$smartyCompileDir = $this->getAbsolutePath($smartyCompileDir);
			
			$this->initSmarty($viewDir,$smartyCompileDir);
			
			foreach($param as $smartName=>$assignValue)
			{
				$this->_smarty->assign($smartName,$assignValue);
			}
			
			$this->_smarty->display($viewFile);
			return;
		}
		else
		{
			CError::handler(E_ERROR,"Smarty.class.php文件不存在,请检查smarty目录是否正确.",__FILE__,__LINE__);
		}
	}
	
	/**
	 * 初始化smarty模板
	 * @param $templateDir view文件存放路径
	 * @param $compileDir smarty产生的编译文件路径
	 */
	private function initSmarty($templateDir,$compileDir)
	{
		$this->_smarty = new Smarty();
		$this->_smarty->compile_check = true;
		$this->_smarty->debugging = false;
		$this->_smarty->caching = false;

		$this->_smarty->template_dir = $templateDir;
		$this->_smarty->compile_dir = $compileDir;

		//$smt->register_function("insert_scripts", "smarty_insert_scripts");
		//$smt->register_function("create_pages", "smarty_create_pages");
		
		//return $smt;
	}
}
?>